
//多条数据的遍历
function numberTravel(data){
	var str = '';
	for(var x in data){
		var row = data[x];
		str += singleTravel(row);
	}
	return str;
}


//单条数据的遍历
function singleTravel(row){
	var str ='<tr>'+
			'<td ><input type="button" id="title" data-id="'+row.id+'" value="'+row.title+'" /></td>'+
			'<td>'+row.user_id+'</td>'+
			'<td>'+row.create_time+'</td>'+
			'<td><input type="button" onclick=del(this) data-id="'+row.id+'" value="删除" /></td>'+
			'<tr>';
	return str;
}


//贴子列表函数，默认显示贴子列表的第一页
function getPostList(currentPage){
	$.get('/index/index/postList',{page:1},function(postList){ 
		var str = numberTravel(postList.data);
		$("#post-list-tab").append(str);
	    var total = postList.total;
	    var per_page = postList.per_page;
	    pageCount = Math.ceil(total/per_page);
	    lastPageCount = total-(pageCount-1)*per_page;
	    page(pageCount,$('#pageIt'));
	},'json');	
}


$(document).ready(function(){
	//登录事件
	$("#login").click(function(){
		$("#loginPage").toggle();
	}); 
	
	//登录确认事件
	$("#loginCommit").click(function(){
		$("#loginPage").toggle();
	    var user_name = $("#user_name").val();
	    var user_password = $("#user_password").val();
	    var identify = $("#identify").val();
	    $.post("/index/Index/login",{user_name, user_password, identify},function(data){
	    	$("#info").text(data.info);
	    	$("#user-unlogin-top").hide();
	    	$("#user-login-top").show();
	    	$("#user-name").attr('value',user_name);
	    	$("img").attr('src',data.thumb);
	    },'json');
	});
	
   $.get("/index/Index/captcha_img",function(data){
	   $("#captcha_img").html(data);
   });

    //注册事件
	$("#regist").click(function(){
		$("#registPage").toggle();
	});
	
	//注册确认事件
	$("#registCommit").click(function(){
		$("#registPage").toggle();
        var user_name = $("#registuser_name").val();
        var user_password = $("#registuser_password").val();
        var image = $("#imagepath").val();
        var thumb = $("#thumbpath").val();
        $.post("/index/Index/regist",{user_name, user_password, image, thumb},function(data){
        	$("#info").text(data.info);
        },'json');
       
    });
    
	//选中文件事件
    $("#file").change(function(){
    	if(!this.files[0]) return;
    	var name = this.name;
    	var formElement = document.getElementById("myForm");
    	var data = new FormData(formElement);
    	data.append('imagename',name);
    	$.ajax({
    		url:'/index/index/upLoad',
    		type:'post',
    		cache:false,
    		data:data,
    		dataType:'json',
            processData:false,
            contentType:false,
    		success:function(info){
    			//$("img").attr('src',info.thumbpath);
    			$("#thumbpath").val(info.thumbpath);
    			$("#imagepath").val(info.imagepath);
    			$("img").attr('src',info.waterpath);
    		}
    		
    	});
    });
    
	//用户详情
    $.get("/index/index/userDetail",function(d){
    	var userDetail = [];
    	userDetail = d.userDetail;
        if(userDetail == null){
            $("#user-unlogin-top").show();
        }else{
        	$("#user-login-top").show();
        	$("#user-name").attr('value',userDetail.user_name);
        }
    },'json');
    
    //退出事件
    $("#quit").click(function(){
    	$.get("/index/index/quit",function(data){
			$("#info").text(data.info);
			$("#user-unlogin-top").show();
	    	$("#user-login-top").hide();
	    });
    });
    
    //发帖事件
    $("#post").click(function(){
        $("#postPage").toggle();
    });
    
    //发帖确认事件
    $("#postCommit").click(function(){
        $("#postPage").toggle();
        title = $("#post-title").val();
        content = $("#post-content").val();
        $.post("/index/Index/post",{title:title,content:content},function(d){
	    	if(d.error == 0){
	    		var str = singleTravel(d.post);
	    		$('#post-list-tab').append(str);
	    		$("#info").text(d.info);
	    	}
	    	
        	if(d.error == 1){
        		$("#info").text(d.info);	
        	}
        },'json');
    });
    
   //贴子列表函数，默认显示贴子列表的第一页
    getPostList(1); 
    
   //分页事件
    $('#pageIt').on("click",'a',function(){ 
    	currentPage = $(this).attr("pagenum");
    	$.post('/index/index/postList',{page:currentPage},function(postList){  
    		var str = numberTravel(postList.data);
    		$("tr").not("#headq").remove();
    		$("#post-list-tab").append(str);
    	},'json');
    });     
    
    //进入详情事件    
    $("#post-list-tab").on("click","#title",function(){
    	var id = $(this).attr("data-id");
    	window.location.href = "postDetail.html?id="+id;
    }); 
});


//贴子删除事件
var del = function(node){
	var id = $(node).attr("data-id");
	$.post('/index/index/postDelete',{id:id},function(postDelete){
			if(postDelete.error == 2){
				$(node).parent().parent().remove();
			    $("#info").text(postDelete.info);
			}
		});	
}	
	
		